package main.ServerTest;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/15 17:36
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
import main.Daoimpl.UserDAOImpl;
import main.JdbcUtils;
import main.Service.UserService;
import main.ServiceImpli.UserServiceImpl;

import main.dao.UserDAO;
import main.pojo.User;
import org.apache.commons.dbutils.DbUtils;
import org.junit.jupiter.api.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class UserServiceImplTest {
    private static Connection connection= JdbcUtils.getConnection();
    private UserService userService=new UserServiceImpl();

    @BeforeAll
    public static void setup() throws SQLException {
        connection = JdbcUtils.getConnection();
        // 创建 users 表并插入测试数据
        try {
            TestUtil.createUsersTable(connection);
            TestUtil.insertTestDataIntoUsers(connection);
        } catch (SQLException e) {
        }
    }

    @BeforeEach
    public void init() {
        UserDAO userDAO = new UserDAOImpl(connection);
        userService = new UserServiceImpl(userDAO);
    }

    @Test
    public void testGetUserByID() throws SQLException {
        int userID = 4;
        User user = userService.getUserByID(userID);
        System.out.println(user.toString());
        assertNotNull(user);
        assertEquals(userID, user.getUserID());
    }

    @Test
    public void testGetUserByUsername()  {
        String username = "阿里木";
        User user = userService.getUserByUsername(username);
        System.out.println(user.toString());
        assertNotNull(user);
        assertEquals(username, user.getUsername());
    }
    @Test
    public  void testCheckLogin(){
        String username = "Am126980";
        User user1=new User();
        user1.setUsername(username);
        user1.setPassword("12345");
        Boolean flag = userService.CheckLogin(user1);
//        assertEquals();
        System.out.println(flag);
    }

    @Test
    public void testGetAllUsers(){
        List<User> users = userService.getAllUsers();
//        for (User user:users){
//            user.getUserID();
//        }
        System.out.println(users.get(1));
        assertNotNull(users);
        assertFalse(users.isEmpty());
        assertEquals(43, users.size());
    }

    @Test
    public void testCreateUser() throws SQLException {
        User user = new User();
        user.setUserID(440);
        user.setNickname("mary");
        user.setPassword("password123");
        user.setUsername("Mary");
        user.setUsername("avatar.jpg");
        userService.createUser(user);
        List<User> users = userService.getAllUsers();
        assertNotNull(users);
        System.out.println(users.get(2));
//        assertEquals(4, users.size());
    }

    @Test
    public void testUpdateUser() throws SQLException {
        int userID = 2;
        String newNickname = "Updated Nickname";
        User user = userService.getUserByID(userID);
        assertNotNull(user);
        user.setNickname(newNickname);
        userService.updateUser(user);
        User updatedUser = userService.getUserByID(userID);
        assertNotNull(updatedUser);
        assertEquals(newNickname, updatedUser.getNickname());
    }

    @Test
    public void testDeleteUser() throws SQLException {
        int userID = 3;
        userService.deleteUser(userID);
        List<User> users = userService.getAllUsers();
        assertNotNull(users);
        assertEquals(5, users.size());
    }
    @AfterAll
    public static void cleanup()   {
        DbUtils.closeQuietly(connection);
    }
}
